package com.example.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.example.Mapper.SalesreturnsDao;
import com.example.pojo.Detail;
import com.example.pojo.Saleinfo;
import com.example.pojo.SalesreturnDetails;
import com.example.pojo.Salesreturns;
import com.example.pojo.Saleunstorage;
import com.example.vo.PageBean;

@Service
public class SalesreturnsBiz {
	
	@Autowired
	private SalesreturnsDao salesreturnsDao;

	public SalesreturnsDao getSalesreturnsDao() {
		return salesreturnsDao;
	}

	public void setSalesreturnsDao(SalesreturnsDao salesreturnsDao) {
		this.salesreturnsDao = salesreturnsDao;
	}
	
	/**
	 * 分页查询
	 * @param pagenum
	 * @param pagesize
	 * @return
	 */
	public PageBean findAll(int pagenum,int pagesize){
		PageBean pb=new PageBean();
		pb.setPagenum(pagenum);
		pb.setPagesize(pagesize);
		pb.setRowcount(salesreturnsDao.selectcount());
		List<Salesreturns> salesreturns = salesreturnsDao.selectSalesreturns((pagenum-1)*pagesize, pagesize);
		pb.setPagedata(salesreturns);
		return pb;
	}
	
	/**
	 * 根据id加载销售退货
	 * @param id
	 * @return
	 */
	public Salesreturns load(String id){
		return salesreturnsDao.loadSalesreturns(id);
	}
	
	/**
	 * 查询销售退货数据的总条数
	 * @return
	 */
	public Integer findcount(){
		return salesreturnsDao.selectcount();
	}
	
	/**
	 * 保存
	 * @param pojo
	 */
	public void savesalesreturns(Salesreturns pojo){
		String xsddid;
		if (salesreturnsDao.loadSalesreturns(pojo.getBillno()) != null) {
			salesreturnsDao.deletexiang(pojo.getBillno());
			salesreturnsDao.deleteSalesreturns(pojo.getBillno());
		}
		if ("是".equals(pojo.getIsdeduct())) {
			salesreturnsDao.insertSalesreturns(pojo);
			for (SalesreturnDetails temp : pojo.getSalesreturnDetails()) {
				temp.setBillno(pojo.getBillno());
				salesreturnsDao.cong(temp);
				xsddid = salesreturnsDao.selectckly(temp.getFromno());
				salesreturnsDao.updatexsddzhu(xsddid);
				salesreturnsDao.updatexsddxiang(temp.getSumqty(), xsddid, temp.getProdId());
			}
		}else {
			salesreturnsDao.insertSalesreturns(pojo);
			for (SalesreturnDetails temp : pojo.getSalesreturnDetails()) {
				temp.setBillno(pojo.getBillno());
				salesreturnsDao.cong(temp);
			}
		}
	}
	
	/**
	 * 点击审核时的变化
	 * @param id
	 */
	public void auditingSalesreturns(String id){
		salesreturnsDao.auditingSalesreturns(id);
		Salesreturns pojo = salesreturnsDao.loadSalesreturns(id);
		for (SalesreturnDetails temp : pojo.getSalesreturnDetails()) {
			Detail d = new Detail();
			d.setSource(id);
			d.setSinglecase("销售退货单");
			d.setWarehouseid(pojo.getWareid());
			d.setNumber(temp.getSumqty());
			d.setPrice(temp.getPrice());
			d.setAmountof(pojo.getTotal());
			salesreturnsDao.insertchuruk(d);
			salesreturnsDao.updatecksl(temp.getSumqty(), pojo.getWareid(), temp.getProdId());
		}
		salesreturnsDao.updatekh(pojo.getTotal(), pojo.getTotal(), pojo.getBilldate(), pojo.getCustoshortid());
	}
	
	/**
	 * 点击反审核时的变化
	 * @param id
	 */
	public void antiauditSalesreturns(String id){
		salesreturnsDao.antiauditSalesreturns(id);
		salesreturnsDao.deletechuruk(id);
		Salesreturns pojo = salesreturnsDao.loadSalesreturns(id);
		for (SalesreturnDetails temp : pojo.getSalesreturnDetails()) {
			salesreturnsDao.fupdatecksl(temp.getSumqty(), pojo.getWareid(), temp.getProdId());
		}
		String date = salesreturnsDao.selectxsthdate();
		salesreturnsDao.fupdatekh(pojo.getTotal(), pojo.getTotal(), date,  pojo.getCustoshortid());
	}
	
	/**
	 * 修改删除状态
	 * @param id
	 */
	public void updatestatus(String id){
		salesreturnsDao.updatestatus(id);
	}
	
	/**
	 * 根据时间查询最大单号
	 * @param date
	 * @return
	 */
	public String finddate(String date){
		String id = salesreturnsDao.selectdate(date);
		if (id==null || id=="") {
			id=date.replace("-", "")+"001";
		}else {
			id=String.valueOf(Long.valueOf(id)+1);
		}
		return id;
	}
	
	/**
	 * 查询销售出库主表
	 * @return
	 */
	public List<Saleunstorage> selectck(){
		return salesreturnsDao.selectck();
	}
	
	/**
	 * 根据id加载出库主祥数据
	 * @param ckid
	 * @return
	 */
	public Saleunstorage loadxsck(String ckid){
		Saleunstorage pojo = salesreturnsDao.loadck(ckid);
		pojo.setInfo(salesreturnsDao.selectckxiang(ckid));
		return pojo;
	}
	
}